package net.qhzw.vip.qiyu.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.qhzw.vip.qiyu.dto.MaintUserDto;
import net.qhzw.vip.qiyu.entity.MaintRpt;
import net.qhzw.vip.qiyu.result.JsonResult;
import net.qhzw.vip.qiyu.service.MaintRptService;
import net.qhzw.vip.qiyu.util.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/maint/user")
@Api(tags = "维修user接口")
public class MaintUserController {


    @Autowired
    private MaintRptService maintRptService;


    @ApiOperation(value = "查询用户是否有派单")
    @GetMapping("maintCout")
    public JsonResult maintCout() {
        Long userId = SecurityUtils.getUserId();
        int count = maintRptService.maintCout(userId);
        return JsonResult.ok(count);
    }

    @ApiOperation(value = "立即接单")
    @PostMapping("takeAnOrdrNow/{id}")
    public JsonResult takeAnOrdrNow(@PathVariable Long id) {
        Long userId = SecurityUtils.getUserId();
        boolean isOK = maintRptService.takeAnOrdrNow(id, userId);
        if (isOK) {
            return JsonResult.ok();
        }
        return JsonResult.fail();
    }

    @ApiOperation(value = "无法接单")
    @PostMapping("unableToTakeOrders/{id}")
    public JsonResult unableToTakeOrders(@PathVariable Long id) {
        Long userId = SecurityUtils.getUserId();
        boolean isOK = maintRptService.unableToTakeOrders(id, userId);
        if (isOK) {
            return JsonResult.ok();
        }
        return JsonResult.fail();
    }

    @ApiOperation(value = "到达报修点")
    @PostMapping("arrive/{id}")
    public JsonResult arrive(@PathVariable Long id) {
        boolean isOK = maintRptService.arrive(id);
        if (isOK) {
            return JsonResult.ok();
        }
        return JsonResult.fail();
    }

    @ApiOperation(value = "维修完成")
    @PostMapping("repairDone/{id}")
    public JsonResult repairDone(@PathVariable Long id) {
        Long userId = SecurityUtils.getUserId();
        boolean isOK = maintRptService.repairDone(id, userId);
        if (isOK) {
            return JsonResult.ok();
        }
        return JsonResult.fail();
    }

    @ApiOperation(value = "服务评价")
    @PostMapping("servCmt")
    public JsonResult servCmt(@RequestBody Map<String, String> parm) {
        boolean isOK = maintRptService.servCmt(parm);
        if (isOK) {
            return JsonResult.ok();
        }
        return JsonResult.fail();
    }

    @ApiOperation(value = "查看维修师傅信息")
    @GetMapping("maintUserInfo/{id}")
    public JsonResult findMaintUserInfo(@PathVariable Long id) {
        MaintUserDto maintUserDto = maintRptService.findMaintUserInfo(id);
        return JsonResult.ok(maintUserDto);
    }


    @ApiOperation(value = "查看维修师傅信息")
    @GetMapping("history")
    public JsonResult history(Page<MaintRpt> page) {
        Long userId = SecurityUtils.getUserId();
        IPage<MaintRpt> rptIPage = maintRptService.page(page,
                new LambdaQueryWrapper<MaintRpt>()
                        .eq(MaintRpt::getRepairUserId, userId)
                        .last("order by created desc"));
        return JsonResult.ok(rptIPage);
    }


}
